package com.wjyf.reducing_bento.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjyf.reducing_bento.common.ErrorCode;
import com.wjyf.reducing_bento.exception.BusinessException;
import com.wjyf.reducing_bento.mapper.InviteRecordMapper;
import com.wjyf.reducing_bento.model.entity.InviteReward;
import com.wjyf.reducing_bento.model.entity.UserBean;
import com.wjyf.reducing_bento.model.vo.InviteRecordVo;
import com.wjyf.reducing_bento.model.vo.InviteRewardVo;
import com.wjyf.reducing_bento.service.InviteRewardService;
import com.wjyf.reducing_bento.mapper.InviteRewardMapper;
import com.wjyf.reducing_bento.service.RewardService;
import com.wjyf.reducing_bento.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author 26439
 * @description 针对表【tb_invite_reward(邀请奖励表)】的数据库操作Service实现
 * @createDate 2023-04-29 19:14:54
 */
@Service
public class InviteRewardServiceImpl extends ServiceImpl<InviteRewardMapper, InviteReward>
        implements InviteRewardService {

    @Resource
    private RewardService rewardService;

    @Resource
    private UserService userService;

    @Resource
    private InviteRewardMapper inviteRewardMapper;

    @Override
    public void saveInviteRewardInfoAndPulsBalance(Long inviterId, Long inviteeId) {
        // 查询奖励配置
        BigDecimal amount = rewardService.getRewardInfo().getAmount();
        InviteReward inviteReward = new InviteReward()
                .setInviterId(inviterId)
                .setInviteeId(inviteeId)
                .setAmount(amount);
        boolean save = this.save(inviteReward);
        if (!save) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        amount = amount.setScale(2, BigDecimal.ROUND_HALF_UP);
        //加金额
        boolean recharge = userService.recharge(amount.doubleValue(), inviterId);
        if (!recharge) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "发放奖励失败");
        }
    }

    @Override
    public Page<InviteRewardVo> userGetInviteRewardList(Long current, Long pageSize, HttpServletRequest request) {
        if (current == null || pageSize == null || current <= 0 || pageSize <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserBean curUser = userService.getUser(request);

        Long total = inviteRewardMapper.selectPageToInviteRewardVoListCount(curUser.getUId());

        List<InviteRewardVo> inviteRecordVoList = inviteRewardMapper.selectPageToInviteRewardVoList(
                current > 0 ? (current - 1) * pageSize : 0,
                pageSize * (current > 0 ? 1 : 0), curUser.getUId());
        Page<InviteRewardVo> rewardVoPage = new Page<>(current, pageSize);
        rewardVoPage.setRecords(inviteRecordVoList)
                .setTotal(total);
        return rewardVoPage;
    }

    @Override
    public Page<InviteRewardVo> adminGetInviteRewardList(Long current, Long pageSize) {
        if (current == null || pageSize == null || current <= 0 || pageSize <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        Long total = inviteRewardMapper.selectPageToInviteRewardVoListCount(null);

        List<InviteRewardVo> inviteRecordVoList = inviteRewardMapper.selectPageToInviteRewardVoList(
                current > 0 ? (current - 1) * pageSize : 0,
                pageSize * (current > 0 ? 1 : 0), null);
        Page<InviteRewardVo> rewardVoPage = new Page<>(current, pageSize);
        rewardVoPage.setRecords(inviteRecordVoList)
                .setTotal(total);
        return rewardVoPage;
    }
}




